package negocio;

import interfaces.AdministradorArticulo;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import dtos.ArticuloDTO;
import entities.Articulo;
import entities.Electrodomestico;
import entities.Moda;
import entities.Mueble;
import entities.Ninio;

/**
 * Session Bean implementation class Articulo
 */
@Stateless
public class AdministradorArticuloBean implements AdministradorArticulo {

	@PersistenceContext
	EntityManager em;
	
	public void registrarArticulo( Articulo a ){
		em.persist(a);
		em.flush();
	}

	@Override
	public List<ArticuloDTO> obtenerArticulos() {
		List<ArticuloDTO> artsDTO = new ArrayList<ArticuloDTO>();
		
		//query de ninios
		Query q = em.createQuery("select s from Articulo s where TYPE(s) = :type").setParameter("type",Ninio.class);
		List<Ninio> ninios = (List<Ninio>)q.getResultList();
		for (Ninio art : ninios) {
			artsDTO.add(art.getDTO());
		}

		//query de electrodomesticos
		q = em.createQuery("select s from Articulo s where TYPE(s) = :type").setParameter("type",Electrodomestico.class);
		List<Electrodomestico> electrodomesticos= (List<Electrodomestico>)q.getResultList();
		for (Electrodomestico art : electrodomesticos) {
			artsDTO.add(art.getDTO());
		}

		//query de muebles
		q = em.createQuery("select s from Articulo s where TYPE(s) = :type").setParameter("type",Mueble.class);
		List<Mueble> muebles= (List<Mueble>)q.getResultList();
		for (Mueble art : muebles) {
			artsDTO.add(art.getDTO());
		}

		//query de moda
		q = em.createQuery("select s from Articulo s where TYPE(s) = :type").setParameter("type",Moda.class);
		List<Moda> modas= (List<Moda>)q.getResultList();
		for (Moda art : modas) {
			artsDTO.add(art.getDTO());
		}
		
		return artsDTO;
	}
	
	public void registrarRanking( List<ArticuloDTO> articulosDTO ) {
		List<Integer> codigosArticulo = new ArrayList<Integer>();
		for (ArticuloDTO art : articulosDTO) {
			codigosArticulo.add(art.getCodigoArticulo());
		}
		Query q = em.createQuery("SELECT a FROM Articulo a where codigo in :codigos");
		q.setParameter("codigos", codigosArticulo);
		List<Articulo> articulos = (List<Articulo>) q.getResultList();
		if ( articulos != null && articulosDTO != null ) {
			for (Articulo articulo : articulos) {
				for (ArticuloDTO artDto : articulosDTO) {
					if ( articulo.getCodigo().intValue() == artDto.getCodigoArticulo().intValue() ) {
						articulo.setPosicion(artDto.getPosicion());
					}
				}
			}
		}
	}

	public List<ArticuloDTO> obtenerArticulosPorOrden(String tipo, String columna, String orden) {
		
		List<ArticuloDTO> artsDTO = new ArrayList<ArticuloDTO>();
		if(! tipo.equals("Ranking")){
			Query q = em.createQuery("select s from Articulo s where TYPE(s) = :type order by s." + columna + " " + orden);
			
			if(tipo.equals("Ninio")){
				q.setParameter("type", Ninio.class);
				List<Ninio> ninios = (List<Ninio>)q.getResultList();
				for (Ninio art : ninios) {
					artsDTO.add(art.getDTO());
				}
				return artsDTO;
				
			}else if(tipo.equals("Mueble")){
				q.setParameter("type", Mueble.class);
				List<Mueble> muebles = (List<Mueble>)q.getResultList();
				for (Mueble art : muebles) {
					artsDTO.add(art.getDTO());
				}
				return artsDTO;
				
			}else if(tipo.equals("Electrodomestico")){
				q.setParameter("type", Electrodomestico.class);
				List<Electrodomestico> electrodomesticos = (List<Electrodomestico>)q.getResultList();
				for (Electrodomestico art : electrodomesticos) {
					artsDTO.add(art.getDTO());
				}
				return artsDTO;
				
			}else if(tipo.equals("Moda")){
				q.setParameter("type", Moda.class);
				List<Moda> modas = (List<Moda>)q.getResultList();
				for (Moda art : modas) {
					artsDTO.add(art.getDTO());
					return artsDTO;
				}
			}
		}
			
		return artsDTO;
		
	}
	
	//Metodo para el Autocomplete
	public  String toJavascriptArray(List<String> lista){
	    StringBuffer sb = new StringBuffer();
	    sb.append("[");
	    for(int i=0; i< lista.size(); i++){
	        sb.append("\"").append(lista.get(i)).append("\"");
	        if(i+1 < lista.size()){
	            sb.append(",");
	        }
	    }
	    sb.append("]");
	    return sb.toString();
	}

	@Override
	public List<ArticuloDTO> busqueda(String valor, String tipo) {
		List<ArticuloDTO> artsDTO = new ArrayList<ArticuloDTO>();
		Query q;
		
		//query de ninios
		if(tipo.equalsIgnoreCase("ninio")){
			q = em.createQuery("select s from Articulo s where TYPE(s) = :type and s.descripcion like :valor").setParameter("type",Ninio.class).setParameter("valor", valor);
			List<Ninio> ninios = (List<Ninio>)q.getResultList();
			for (Ninio art : ninios) {
				artsDTO.add(art.getDTO());
			}
			return artsDTO;
			
		}else if(tipo.equalsIgnoreCase("electrodomestico")){
			
			q = em.createQuery("select s from Articulo s where TYPE(s) = :type and s.descripcion like :valor").setParameter("type",Electrodomestico.class).setParameter("valor", valor);
			List<Electrodomestico> electrodomesticos= (List<Electrodomestico>)q.getResultList();
			for (Electrodomestico art : electrodomesticos) {
				artsDTO.add(art.getDTO());
			}
			return artsDTO;

			
		}else if(tipo.equalsIgnoreCase("mueble")){

			q = em.createQuery("select s from Articulo s where TYPE(s) = :type and s.descripcion like :valor").setParameter("type",Mueble.class).setParameter("valor", valor);
			@SuppressWarnings("unchecked")
			List<Mueble> muebles= (List<Mueble>)q.getResultList();
			for (Mueble art : muebles) {
				artsDTO.add(art.getDTO());
			}
			return artsDTO;
	

		}else if(tipo.equalsIgnoreCase("moda")){

			q = em.createQuery("select s from Articulo s where TYPE(s) = :type and s.descripcion like :valor").setParameter("type",Moda.class).setParameter("valor", valor);
			List<Moda> modas= (List<Moda>)q.getResultList();
			for (Moda art : modas) {
				artsDTO.add(art.getDTO());
			}

			return artsDTO;
		}
		
		return null;
	}

}
